<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/Im/getToken/ 获取im实时聊天功能token
 * @apiName getToken
 * @apiGroup Im
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Im/createGroup/ 创建聊天室
 * @apiName createGroup
 * @apiGroup Im
 *
 * @apiParam {String} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;

use think\Page;
use think\Db;
use think\Model;
use \think\Log;
use app\common\controller\Api;

class Im extends Base
{
    private $ak = "8w7jv4qb82eay";
    private $sk = "MAgKhbgnol2";

    //获取token
    public function getToken()
    {
        $uid = I('user_id');
        // 从数据库中获取token    
        $token = M('im_oauth')->where('uid='.$uid)->find();
        // 如果有token就返回    
        if ($token){
            $return = codemsg(5003);
            $return['data'] = $token;
            Log::api('【Im/getToken】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($uid <= 0 || empty($uid)) {
            $return = codemsg(-1);
            Log::api('【Im/getToken】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        // 获取用户昵称和头像    
        $wherefind['user_id'] = array('eq',$uid);
        $user_data = M('users')->field('nickname,head_pic')->where($wherefind)->find();
        //用户不存在    
        if (empty($user_data)) {
            $return = codemsg(-4003);
            Log::api('【Im/getToken】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $heardimg = LOC_PATH.$user_data['head_pic'];
        // 实例化融云  
        vendor('imapi.rongcloud');
        $rong_cloud = new \RongCloud($this->ak, $this->sk);
        // 获取token    
        $token_json = $rong_cloud->user()->getToken($uid,$user_data['nickname'],$heardimg);
        $token_array = json_decode($token_json,true);
        // 获取token失败    
        if ($token_array['code'] != 200) {
            $return = codemsg(-7048);
            Log::api('【Im/getToken】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $token = $token_array['token'];
        $data = array(
            'uid' => $uid,
            'type' => 1,
            'nickname' => $user_data['nickname'],
            'head_img' => $user_data['head_pic'],
            'access_token' => $token
        );
        // 插入数据库    
        $result = M('im_oauth')->insert($data);

        if ($data){
            $return = codemsg(5003);
            $return['data'] = $data;
            Log::api('【Im/getToken】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }else{
            $return = codemsg(-7028);
            Log::api('【Im/getToken】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }
    }

    //创建聊天室
    public function createGroup(){
        $uid = I('post.user_id');
        //查询用户是否存在
        $where['user_id'] = array('eq',$uid);
        $where['is_lock'] = array('eq',0);
        $userfind = M('users')->where($where)->find();
        if (empty($userfind)){
            $return = codemsg(-4003);
            Log::api('【Im/createGroup】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //查询直播间是否存在
        $whereroom['uid'] = array('eq',$uid);
        $findroom = M('video_room')->where($whereroom)->find();
        if ($findroom){
            $map['updatetime'] = time();
            $update = M('video_room')->where($whereroom)->update($map);
            //创建成功
            $return = codemsg(3);
            Log::api('【Im/getToken】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }
        $map['uid'] = $uid;
        $map['groupname'] = $uid.'的直播间';
        $map['createtime'] = time();
        $add = M('video_room')->insertGetId($map);
        if($add > 0){
            //创建成功
            $return = codemsg(3);
            Log::api('【Im/getToken】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }else{
            $return = codemsg(-7049);
            Log::api('【Im/createGroup】-[返回]--'.json_encode($return));
            return json_encode($return);die;
        }
    }

//    //加入聊天室
//    public function joinRoom(){
//        $uid = I('post.user_id');
//        $groupRoomId = I('post.groupRoomId');
//        $uid = 14;
//        $groupRoomId = 17;
//        //查询是否存在该直播间
//        $where['id'] = array('eq',$groupRoomId);
//        $findroom = M('video_room')->where($where)->find();
//        if (empty($findroom)){
//            $return = codemsg(-7050);
//            Log::api('【Im/joinRoom】-[返回]--'.json_encode($return));
//            return json_encode($return);die;
//        }
//        //查询该用户是否存在
//        $findwhere['user_id'] = array('eq',$uid);
//        $finduser = M('users')->where($findwhere)->find();
//        if (empty($finduser)){
//            $return = codemsg(-4003);
//            Log::api('【Im/joinRoom】-[返回]--'.json_encode($return));
//            return json_encode($return);die;
//        }
//        //加入直播间
//        vendor('imapi.rongcloud');
//        $RongCloud = new \RongCloud($this->ak, $this->sk);
//        $joinuid = [$uid];
//        $result =  json_decode($RongCloud->chatroom()->join($joinuid,$findroom['uid']),JSON_FORCE_OBJECT );
//        dump($result);
//    }
//
//    //查询聊天室内用户
//    public function roomUserList(){
//        $groupRoomId = I('post.groupRoomId');
//        $groupRoomId = 17;
////        $where['id'] = array('eq',$groupRoomId);
////        $findroom = M('video_room')->where($where)->find();
////        if (empty($findroom)){
////            $return = codemsg(-7050);
////            Log::api('【Im/roomUserList】-[返回]--'.json_encode($return));
////            return json_encode($return);die;
////        }
//        //获取聊天室内用户
//        vendor('imapi.rongcloud');
//        $RongCloud = new \RongCloud($this->ak, $this->sk);
//        $result =  json_decode($RongCloud->chatroom()->queryUser($groupRoomId,500,2),JSON_FORCE_OBJECT );
//        dump($result);
//    }


}